package util.util;

import util.model.common.ExcelInfo;
import util.model.common.FieldInfo;
import util.model.common.TableInfo;
import util.model.mysql.AddSql;
import util.util.excel.ReadExcel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: liW
 * @date: 2020/9/3 11:17
 */
public class DevModelUtil {

    /**
     * mysql 数据库表名 -》 实体名
     *
     * @param tableDBName 数据库表名
     * @return 实体名
     */
    public static String dBName2ResVoName(String tableDBName) {
        tableDBName = tableDBName.replace("t_", "").replace("inf_", "");
        return DevComUtil.line2HumpFristUp(tableDBName) + "ResVO";
    }

    /**
     * mysql 数据库表名 -》 实体名
     *
     * @param tableDBName 数据库表名
     * @return 实体名
     */
    public static String dBName2ReqVoName(String tableDBName) {
        tableDBName = tableDBName.replace("t_", "").replace("inf_", "");
        return DevComUtil.line2HumpFristUp(tableDBName) + "ReqVO";
    }

    /**
     * mysql 数据库表名 -》 实体名
     *
     * @param tableDBName 数据库表名
     * @return 实体名
     */
    public static String dBName2BeanName(String tableDBName) {

        StringBuilder sb = new StringBuilder();
        String[] s = tableDBName.split("_");
        for (String s1 : s) {
            if ("t".equals(s1)) {
                continue;
            }
            if ("inf".equals(s1)) {
                continue;
            }
            if ("sys".equals(s1)) {
                continue;
            }
            sb.append(s1);
            sb.append("_");
        }
        return DevComUtil.line2HumpFristUp(sb.toString()) + "Entity";
    }

    /**
     * mysql 数据库表名 -》 实体名
     *
     * @param tableDBName 数据库表名
     * @return 实体名
     */
    public static String dBName2DaoName(String tableDBName) {
        tableDBName = tableDBName.replace("t_", "").replace("inf_", "");
        return "I" + DevComUtil.line2HumpFristUp(tableDBName) + "Dao";
    }

    /**
     * MySQL  创建实体
     *
     * @param mysqlField 字段信息
     * @return 实体内容
     */
    public static String createMysqlVo(FieldInfo mysqlField) {

        String fieldDBName = mysqlField.getFieldDBName();


        if (voList.contains(fieldDBName)) {
            return "";
        }
        String fieldTitle = mysqlField.getFieldTitle();
        String fieldDBType = mysqlField.getFieldDBType();
        String type;
        if ("tinyint".equals(fieldDBType)){
            type = tinyintMap.get(mysqlField.getFieldLength());
        } else {
            type = mysqlTypeMap.get(fieldDBType);
        }

        if (DevComUtil.isEmpty(type)) {
            System.out.println("没有类型：" + fieldDBType);
            return "";
        }
        String name = DevComUtil.line2Hump(fieldDBName);
        String value = null;
            value = "\n" +
                    "  @ApiModelProperty(value = \"%s\")\n" +
                    "  private %s %s;\n";

        return String.format(value, fieldTitle, type, name);
    }

    /**
     * MySQL  创建实体
     *
     * @param mysqlField 字段信息
     * @return 实体内容
     */
    public static String createMysqlModel(FieldInfo mysqlField) {

        String fieldDBName = mysqlField.getFieldDBName();

        if (baseBeanList.contains(fieldDBName)) {
            return "";
        }
        String fieldTitle = mysqlField.getFieldTitle();
        String fieldDBType = mysqlField.getFieldDBType();

        String type;
        if ("tinyint".equals(fieldDBType)){
            type = tinyintMap.get(mysqlField.getFieldLength());
        } else {
            type = mysqlTypeMap.get(fieldDBType);
        }

        if (DevComUtil.isEmpty(type)) {
            System.out.println("没有类型：" + fieldDBType);
            return "";
        }

        String name = DevComUtil.line2Hump(fieldDBName);
        String value = null;
        if ("tenant_id".equals(fieldDBName)) {
            value = "\n" +
                    "  /**\n" +
                    "   * %s\n" +
                    "   */\n" +
                    "  @TableField(exist = false)\n" +
                    "  private %s %s;\n";
        } else {
            value = "\n" +
                    "  /**\n" +
                    "   * %s\n" +
                    "   */\n" +
                    "  private %s %s;\n" +
                    "  @TableField(exist = false)\n" +
                    "  public static final String %s_FIELD = \"%s\";\n";
        }
        return String.format(value, fieldTitle, type, name, fieldDBName.toUpperCase(), fieldDBName);
    }


    /**
     * MySQL -- sql  -- 主键定义字段
     *
     * @param list 字段信息
     * @return 主键定义字段
     */
    public static String createPk(List<FieldInfo> list) {
        String collect = list.stream().filter(item -> !DevComUtil.isEmpty(item.getFieldPK()))
                .map(item -> item.getFieldDBName()).collect(Collectors.joining(","));
        String value = "PRIMARY KEY (`%s`) USING BTREE";
        return String.format(value, collect);
    }

    /**
     * MySQL -- sql -- 字段sql拼接
     *
     * @param mysqlField 字段信息
     * @return sql
     */
    public static String createMysql(FieldInfo mysqlField) {
        String fieldDBName = mysqlField.getFieldDBName();
        String fieldTitle = mysqlField.getFieldTitle();
        String fieldLength = mysqlField.getFieldLength();
        String fieldScale = mysqlField.getFieldScale();
        String fieldDBType = mysqlField.getFieldDBType();
        String fieldDBMust = mysqlField.getFieldDBMust();
        String fieldDefaultValue = mysqlField.getFieldDefaultValue();
        String fieldPK = mysqlField.getFieldPK();

//        String auto = "";
//
//        // 自增
//        if (!DevComUtil.isEmpty(fieldPK)) {
//            auto = " AUTO_INCREMENT ";
//        }

        // 小数位数
        if (DevComUtil.notEmpty(fieldScale) && !"0".equals(fieldScale)) {
            fieldScale = "," + fieldScale;
        } else {
            fieldScale = "";
        }

        // 默认值
        if (DevComUtil.isEmpty(fieldDefaultValue)) {
            if (DevComUtil.isEmpty(fieldDBMust)) {
                fieldDefaultValue = " DEFAULT NULL ";
            } else {
                fieldDefaultValue = "";
            }
        } else {
            fieldDefaultValue = " DEFAULT " + fieldDefaultValue;
        }

        // 必填
        if (!DevComUtil.isEmpty(fieldDBMust)) {
            fieldDBMust = " NOT NULL ";
        } else {
            fieldDBMust = "NULL";
        }

        // 注释
        if (DevComUtil.isEmpty(fieldTitle)) {
            fieldTitle = "";
        } else {
            fieldTitle = " COMMENT '" + fieldTitle + "'";
        }

        if ("datetime".equals(fieldDBType)) {
            fieldLength = "6";
        }

        String value = "`%s` %s(%s%s) %s %s %s,";

        String sql = String.format(value, fieldDBName, fieldDBType, fieldLength, fieldScale, fieldDBMust, fieldDefaultValue, fieldTitle);

        if ("datetime".equals(fieldDBType)) {
            sql = sql.replace("datetime(6)", "datetime");
        }
        return sql;
    }

    /**
     * MySQL 获取创建的实体的信息
     *
     * @param path excel路径
     * @return 实体信息
     */
    public static TableInfo getTableInfo(String path) {
        ExcelInfo<AddSql> excelInfo = new ExcelInfo<>(AddSql.class, path, 2, 1);
        List<AddSql> addSqlModels = ReadExcel.read2Model(excelInfo);
        String s = ReadExcel.readName(excelInfo);
        String[] split = s.split("-");

        List<FieldInfo> fieldInfoList = DevBeanUtil.fieldInfoList(addSqlModels);
        TableInfo tableInfo = new TableInfo();

        tableInfo.setFieldList(fieldInfoList);
        tableInfo.setTableDBName(split[0]);
        tableInfo.setTableTitle(split[1]);
        return tableInfo;
    }



    /**
     * MySQL 获取创建的实体的信息
     *
     * @param path excel路径
     * @return 实体信息
     */
    public static TableInfo getTableInfo(String path, int sheetPage, int startRow) {
        ExcelInfo<AddSql> excelInfo = new ExcelInfo<>(AddSql.class, path, sheetPage, startRow);
        List<AddSql> addSqlModels = ReadExcel.read2Model(excelInfo);
        String s = ReadExcel.readName(excelInfo);
        String[] split = s.split("-");

        List<FieldInfo> fieldInfoList = DevBeanUtil.fieldInfoList(addSqlModels);
        TableInfo tableInfo = new TableInfo();

        tableInfo.setFieldList(fieldInfoList);
        tableInfo.setTableDBName(split[0]);
        tableInfo.setTableTitle(split[1]);
        return tableInfo;
    }


    public static final Map<String, String> oracleTypeMap = new HashMap<String, String>() {{
        put("NUMBER", "BigDecimal");
        put("CHAR", "String");
        put("VARCHAR2", "String");
        put("TIMESTAMP(6)", "Date");
        put("DATE", "Date");
    }};


    // [char, bigint, varchar, decimal, datetime, tinyint, int, longtext, text, time, date,mediumtext,
    // timestamp, set, enum, float, longblob, mediumblob, smallint, double, blob, bit, mediumint]
    /**
     * mysql对应关心
     */
    public static final Map<String, String> mysqlTypeMap = new HashMap<String, String>() {{

        put("tinyint", "Boolean");

        put("int", "Integer");
        put("bigint", "Long");
        put("decimal", "BigDecimal");

        put("char", "String");
        put("varchar", "String");
        put("text", "String");
        put("longtext", "String");
        put("mediumtext", "String");

        put("date", "LocalDateTime");
        put("time", "LocalDateTime");
        put("datetime", "LocalDateTime");
    }};

    public static final Map<String, String> tinyintMap = new HashMap<String, String>() {{
        put("1", "Boolean");
        put("2", "Integer");
        put("3", "Integer");
        put("4", "Integer");
    }};

    /**
     * mysql实体过滤字段
     */
    public static final List<String> baseBeanList = new ArrayList<String>() {{
        add("id");
        add("create_time");
        add("owner_id");
        add("update_time");
        add("update_id");
        add("delete_flag");
        add("enable_flag");
        add("backup_flag");
        add("version");
//        add("tenant_id");
    }};


    /**
     * mysql实体过滤字段
     */
    public static final List<String> voList = new ArrayList<String>() {{
        add("create_time");
        add("owner_id");
        add("update_time");
        add("update_id");
        add("delete_flag");
        add("enable_flag");
        add("backup_flag");
        add("version");
//        add("tenant_id");
    }};


    public static final Map<String, String> baseBeanMap = new HashMap<String, String>() {{
        put("delete_flag" , "0");
        put("enable_flag" , "1");
        put("backup_flag" , "0");
        put("version" , "0");
//        add("tenant_id");
    }};

}
